package com.etc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

/**
 * 数据库通用工具,返回连接,通用的增加,删除,修改的操作,查询,释放资源
 * @author admin
 *
 */
public class DBUtil {
	private static final String url="jdbc:mysql://localhost:3306/cmsdb";//主机名,端口号,数据库名
	private static final String user="root";
	private static final String password="root";

	/**
	 * 返回一个连接对象
	 * @return  连接对象
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public static Connection getConn() throws SQLException, ClassNotFoundException {
		//加载驱动(可以省略)
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection(url, user, password);
	}
	/**
	 * 通用的增加 删除 修改
	 * @param sql 
	 * @param params 补齐占位符的个数
	 * @return true 更新成功  false更新失败
	 */
	public static boolean executeUpdate(String sql,Object...params ) {
		Connection conn=null;
		PreparedStatement pstm=null;
		int n =0;
		try {
			//1.获取连接对象
			conn=getConn();
			//2.通过connection对象 得到PreparedStatement
			pstm = conn.prepareStatement(sql);
			//3.给?赋值
			setPreparedStatement(pstm, params);
			//4.执行sql
			n = pstm.executeUpdate();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//释放资源
			closeAll(null,pstm,conn);
		}
		return n>0;

	}

	/**
	 * 补齐占位符的?的值
	 * @param pstm PreparedStatement 对象
	 * @param params 可变参数(数组)
	 */
	public static void setPreparedStatement(PreparedStatement pstm,Object...params) {
		if (pstm!=null && params!=null) {
			for (int i = 0; i < params.length; i++) {
				try {
					pstm.setObject(i+1, params[i]);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 查询
	 */
	public static CachedRowSet execQuery(String sql,Object... params) {
		Connection conn =null;
		PreparedStatement pstm=null;
		ResultSet rs =null;
		CachedRowSet crs =null;
		try {
			//1.获取连接
			conn = getConn();
			//2.通过connection对象 得到PreparedStatement
			pstm = conn.prepareStatement(sql);
			//3.给?赋值
			setPreparedStatement(pstm, params);
			//4.执行sql
			rs = pstm.executeQuery();
			//5.创建一个离线结果集
			crs = new CachedRowSetImpl();
			//6.使用取自给定 ResultSet 对象的数据填充此 CachedRowSet 对象。
			crs.populate(rs);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			closeAll(rs, pstm, conn);
		}
		return crs;
	}

	/**
	 * 释放资源
	 * @param pstm
	 * @param conn
	 */
	public static void closeAll(ResultSet rs,PreparedStatement pstm,Connection conn) {
		if (null!=rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//如果为空,会报空指针异常
		}

		if (null!=pstm) {
			try {
				pstm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//如果为空,会报空指针异常
		}

		//连接放在最后来释放
		if (null!=conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
